﻿<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	xmlns:commands="clr-namespace:SqlServerRunnerNet.Commands"
	xmlns:viewModel="clr-namespace:SqlServerRunnerNet.ViewModel"
	xmlns:converters="clr-namespace:SqlServerRunnerNet.Infrastructure.Converters"
	mc:Ignorable="d" x:Class="SqlServerRunnerNet.MainWindow"
	Title="SQL Runner .NET" Height="480" Width="640" Icon="run.ico"
	Loaded="Window_OnLoaded" Closing="Window_Closing" UseLayoutRounding="True"
	d:DataContext="{d:DesignInstance viewModel:SqlServerRunnerViewModel}">
	<Window.Resources>
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary Source="MainResourcesDictionary.xaml" />
			</ResourceDictionary.MergedDictionaries>
			<converters:BoolToVisibilityConverter x:Key="VisibilityConverter" />
			<converters:InvertBooleanConverter x:Key="InvertBooleanConverter" />
			<converters:TaskbarItemProgressStateConverter x:Key="ProgressStateConverter" />
			<converters:FractionConverter x:Key="FractionConverter" />
		</ResourceDictionary>
	</Window.Resources>
	<Window.CommandBindings>
		<CommandBinding Command="commands:ApplicationCommands.ExitCommand" Executed="ExitCommand_Executed" />
		<CommandBinding Command="commands:ScriptsCommands.ClearOutputCommand" Executed="ClearOutputCommand_Executed" CanExecute="ClearOutputCommand_CanExecute" />
	</Window.CommandBindings>
	<Window.TaskbarItemInfo>
		<TaskbarItemInfo>
			<TaskbarItemInfo.ProgressValue>
				<MultiBinding Converter="{StaticResource FractionConverter}">
					<Binding Path="CurrentScriptsCount" />
					<Binding Path="TotalScriptsCount" />
				</MultiBinding>
			</TaskbarItemInfo.ProgressValue>
			<TaskbarItemInfo.ProgressState>
				<MultiBinding Converter="{StaticResource ProgressStateConverter}" Mode="OneWay">
					<Binding Path="ExecutionInProgress" />
					<Binding Path="HasTotalScriptsCountBeenDetermined" />
				</MultiBinding>
			</TaskbarItemInfo.ProgressState>
		</TaskbarItemInfo>
	</Window.TaskbarItemInfo>
	<Grid>
		<Grid.RowDefinitions>
			<RowDefinition Height="Auto" />
			<RowDefinition Height="*" />
		</Grid.RowDefinitions>
		<Menu Grid.Row="0">
			<MenuItem Header="File">
				<MenuItem Command="commands:ApplicationCommands.ExitCommand" />
			</MenuItem>
		</Menu>
		<Grid Grid.Row="1">
			<Grid.RowDefinitions>
				<RowDefinition Height="Auto" />
				<RowDefinition Height="Auto" />
				<RowDefinition Height="*" />
			</Grid.RowDefinitions>
			<GroupBox Header="SQL Server Connection" Grid.Row="0" Grid.Column="0">
				<Grid>
					<Grid.ColumnDefinitions>
						<ColumnDefinition Width="Auto" />
						<ColumnDefinition />
						<ColumnDefinition Width="Auto" />
					</Grid.ColumnDefinitions>
					<Label Grid.Column="0" VerticalContentAlignment="Center" Content="Connection String:"/>
					<TextBox Grid.Column="1" Text="{Binding ConnectionString}" />
					<Button Grid.Column="2" Margin="5,0,0,0" Padding="10,0" Command="{Binding BrowseConnectionStringCommand}" Content="..."/>
				</Grid>
			</GroupBox>
			<GroupBox Header="Script files" Grid.Row="1">
				<Grid>
					<Grid.ColumnDefinitions>
						<ColumnDefinition Width="*" />
						<ColumnDefinition Width="Auto" />
					</Grid.ColumnDefinitions>
					<Grid Grid.Column="0" Height="{Binding ActualHeight, ElementName=ButtonsStackPanel}">
						<Grid.RowDefinitions>
							<RowDefinition Height="Auto" />
							<RowDefinition Height="*" />
						</Grid.RowDefinitions>
						<CheckBox Grid.Row="0" Content="Check/Uncheck all folders in the list" Margin="3"
								  IsChecked="{Binding AllScriptsChecked}" IsThreeState="False">
							<CheckBox.Style>
								<Style>
									<Style.Triggers>
										<DataTrigger Binding="{Binding Scripts.Count}" Value="0">
											<Setter Property="CheckBox.IsEnabled" Value="False" />
										</DataTrigger>
									</Style.Triggers>
								</Style>
							</CheckBox.Style>
						</CheckBox>
						<ListBox Grid.Row="1" DataContext="{Binding Scripts}" ItemsSource="{Binding}"
								 x:Name="ScriptsList" ScrollViewer.CanContentScroll="False" SelectionMode="Extended" >
							<ListBox.ItemContainerStyle>
								<Style TargetType="{x:Type ListBoxItem}">
									<Setter Property="IsSelected" Value="{Binding IsSelected}" />
								</Style>
							</ListBox.ItemContainerStyle>
							<ListBox.ItemTemplate>
								<DataTemplate DataType="viewModel:ScriptsFolderViewModel">
									<StackPanel Orientation="Horizontal">
										<CheckBox IsChecked="{Binding IsChecked}" Margin="0,0,3,0" Focusable="False" />
										<TextBlock Text="{Binding FilePath}" />
									</StackPanel>
								</DataTemplate>
							</ListBox.ItemTemplate>
						</ListBox>
					</Grid>
					<StackPanel Grid.Column="1" VerticalAlignment="Top" x:Name="ButtonsStackPanel" Margin="5,0,0,0">
						<Button Command="{Binding AddScriptCommand}" Content="Add" />
						<Button Command="{Binding RemoveScriptCommand}" Content="Remove"/>
						<Button Command="{Binding ClearScriptsCommand}" Content="Clear"/>
						<Grid>
							<Grid.ColumnDefinitions>
								<ColumnDefinition />
								<ColumnDefinition />
							</Grid.ColumnDefinitions>
							<Button Grid.Column="0" Content="Up" Margin="3,1.5,1.5,1.5" Command="{Binding MoveScriptUpCommand}"/>
							<Button Grid.Column="1" Content="Down" Margin="1.5,1.5,3,1.5" Command="{Binding MoveScriptDownCommand}"/>
						</Grid>
						<Button Command="{Binding RunSelectedScriptsCommand}" Content="Run Selected" />
					</StackPanel>
				</Grid>
			</GroupBox>
			<GroupBox Header="Execution log" Grid.Row="2">
				<Grid>
					<Grid.RowDefinitions>
						<RowDefinition Height="Auto" />
						<RowDefinition Height="*" />
						<RowDefinition Height="Auto" />
					</Grid.RowDefinitions>
					<Grid.ColumnDefinitions>
						<ColumnDefinition Width="2*"/>
						<ColumnDefinition Width="Auto"/>
						<ColumnDefinition Width="3*"/>
					</Grid.ColumnDefinitions>
					<ProgressBar Grid.Row="0" Grid.ColumnSpan="3" Height="5" Margin="0,2" IsIndeterminate="{Binding HasTotalScriptsCountBeenDetermined, Converter={StaticResource InvertBooleanConverter}}"
								 Visibility="{Binding ExecutionInProgress, Converter={StaticResource VisibilityConverter}}"
								 Minimum="0" Maximum="{Binding TotalScriptsCount}" Value="{Binding CurrentScriptsCount}"/>
					<TreeView Grid.Row="1" Grid.Column="0" x:Name="ExecutedScriptsTreeView" ItemsSource="{Binding}"
							  DataContext="{Binding ExecutedScripts}" >
						<TreeView.Resources>
							<HierarchicalDataTemplate DataType="{x:Type viewModel:FolderViewModel}" ItemsSource="{Binding Scripts}">
								<StackPanel Orientation="Horizontal">
									<Grid Margin="5,0,2,0">
										<Image Source="/SqlServerRunnerNet;component/Images/folder-closed.png" x:Name="nodeImg" />
										<Image Source="/SqlServerRunnerNet;component/Images/error-overlay.png" HorizontalAlignment="Left" VerticalAlignment="Bottom" x:Name="imgError" Visibility="Hidden" />
									</Grid>
									<TextBlock Text="{Binding DisplayName}" />
								</StackPanel>
								<HierarchicalDataTemplate.Triggers>
									<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
										<Setter TargetName="nodeImg" Property="Source" Value="/SqlServerRunnerNet;component/Images/folder-open.png" />
									</DataTrigger>
									<DataTrigger Binding="{Binding HasError}" Value="True">
										<Setter TargetName="imgError" Property="Visibility" Value="Visible" />
									</DataTrigger>
								</HierarchicalDataTemplate.Triggers>
							</HierarchicalDataTemplate>
							<DataTemplate DataType="{x:Type viewModel:ScriptViewModel}">
								<StackPanel Orientation="Horizontal">
									<Grid Margin="5,0,2,0">
										<Image Source="/SqlServerRunnerNet;component/Images/script.png" />
										<Image Source="/SqlServerRunnerNet;component/Images/error-overlay.png" HorizontalAlignment="Left" VerticalAlignment="Bottom" x:Name="imgError" Visibility="Hidden" />
									</Grid>
									<TextBlock Text="{Binding DisplayName}" />
								</StackPanel>
								<DataTemplate.Triggers>
									<DataTrigger Binding="{Binding HasError}" Value="True">
										<Setter TargetName="imgError" Property="Visibility" Value="Visible" />
									</DataTrigger>
								</DataTemplate.Triggers>
							</DataTemplate>
						</TreeView.Resources>
					</TreeView>
					<Button Grid.Column="0" Grid.Row="2" Margin="0,5,0,0" Command="commands:ScriptsCommands.ClearOutputCommand" Content="{Binding Command.(RoutedUICommand.Name), Mode=OneWay, RelativeSource={RelativeSource Self}}"/>
					<GridSplitter Grid.Row="1" Grid.RowSpan="2" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" Width="5" Margin="5,0" Focusable="False"/>
					<ContentControl Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" DataContext="{Binding ElementName=ExecutedScriptsTreeView, Path=SelectedItem}" Content="{Binding}" Focusable="False">
						<ContentControl.Resources>
							<DataTemplate DataType="{x:Type viewModel:FolderViewModel}">
								<Grid>
									<Grid.RowDefinitions>
										<RowDefinition Height="Auto" />
										<RowDefinition Height="5" />
										<RowDefinition Height="Auto" />
										<RowDefinition Height="5" />
										<RowDefinition Height="Auto" />
									</Grid.RowDefinitions>
									<Grid.ColumnDefinitions>
										<ColumnDefinition Width="Auto" />
										<ColumnDefinition Width="5" />
										<ColumnDefinition Width="*" />
									</Grid.ColumnDefinitions>
									<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
										<Grid.ColumnDefinitions>
											<ColumnDefinition Width="*" />
											<ColumnDefinition Width="Auto" />
										</Grid.ColumnDefinitions>
										<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path}" TextTrimming="CharacterEllipsis" FontWeight="Bold" FontSize="16" />
										<StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="1">
											<Button Command="{Binding CopyPathCommand}">Copy Path</Button>
											<Button Command="{Binding OpenInShellCommand}">Browse</Button>
										</StackPanel>
									</Grid>
									<TextBlock Grid.Row="2" Grid.Column="0" FontSize="14" Margin="20,0,0,0">Passed scripts:</TextBlock>
									<TextBlock Grid.Row="4" Grid.Column="0" FontSize="14" Margin="20,0,0,0">Failed scripts:</TextBlock>
									<TextBlock Grid.Row="2" Grid.Column="2" FontSize="14" FontWeight="Bold" Text="{Binding SucceededScriptsCount}" Foreground="Green"/>
									<TextBlock Grid.Row="4" Grid.Column="2" FontSize="14" FontWeight="Bold" Text="{Binding FailedScriptsCount}" Foreground="Red"/>
								</Grid>
							</DataTemplate>
							<DataTemplate DataType="{x:Type viewModel:ScriptViewModel}">
								<Grid>
									<Grid.RowDefinitions>
										<RowDefinition Height="Auto" />
										<RowDefinition Height="*" />
									</Grid.RowDefinitions>
									<Grid.ColumnDefinitions>
										<ColumnDefinition Width="*" />
										<ColumnDefinition Width="Auto" />
									</Grid.ColumnDefinitions>
									<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path}" TextTrimming="CharacterEllipsis" FontWeight="Bold" FontSize="16" VerticalAlignment="Center" />
									<StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="1">
										<Button Command="{Binding CopyPathCommand}">Copy Path</Button>
										<Button Command="{Binding OpenInShellCommand}">Open</Button>
									</StackPanel>
									<TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" x:Name="NoIssuesBlock">No issues</TextBlock>
									<TextBlock Visibility="Hidden" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding ErrorMessage}" x:Name="ErrorMessageBlock"></TextBlock>
								</Grid>
								<DataTemplate.Triggers>
									<DataTrigger Binding="{Binding HasError}" Value="True">
										<Setter TargetName="NoIssuesBlock" Property="Visibility" Value="Hidden" />
										<Setter TargetName="ErrorMessageBlock" Property="Visibility" Value="Visible" />
									</DataTrigger>
								</DataTemplate.Triggers>
							</DataTemplate>
						</ContentControl.Resources>
					</ContentControl>
				</Grid>
			</GroupBox>
		</Grid>
	</Grid>
</Window>
